declare function
deviceType();
class SoundDeal {
	private soundArr = [];
	public volume: number;
	public soundBtn: any;
	public soundBgm;


	public isGameSound;
	public isBgmSound;
	private context: any;
	public constructor(context: any) {
		this.context = context;
		this.volume = PreferenceData.getSound() / 100;
		this.isBgmSound = 0;
		this.isGameSound = 0;
		//console.log("bgm",this.isBgmSound);
		//console.log("game",this.isGameSound);
	}

	public isOpen() {
		if (this.isGameSound==1 || this.isBgmSound==1) {
			return true;
		} else {
			return false;
		}
	}

	public setCtrDisplay() {
		if (this.isOpen()) {
			this.soundBtn.source = "m_sound_png";
		} else {
			this.soundBtn.source = "m_sound_close_png";
		}
	}

	public openSoundCtr() {
		this.isGameSound = 1;
		this.isBgmSound = 1;
		PreferenceData.setMusicSwitch(1);
		PreferenceData.setSoundSwitch(1);
	}

	public closeSoundCtr() {
		this.isGameSound = 0;
		this.isBgmSound = 0;
		PreferenceData.setMusicSwitch(0);
		PreferenceData.setSoundSwitch(0);
	}

	public playBgm() {
		if (this.soundArr["bgm"]) { return; }
		this.soundBgm = this.soundArr["bgm"] = new BGM("bgm_mp3", this.volume);
		this.isBgmSound = 1;
		PreferenceData.setMusicSwitch(1);
		this.setCtrDisplay();
	}

	public playBgm1() {
		if (this.isBgmSound == 0) return;
		this.soundBgm = this.soundArr["bgm"] = new BGM("bgm_mp3", this.volume);
		this.isBgmSound = 1;
		PreferenceData.setMusicSwitch(1);
	}

	public closeBgm() {
		if (!this.soundBgm) {
			this.soundBgm = null;
			this.setCtrDisplay();
			return;
		}
		this.soundArr["bgm"].stop();
		delete this.soundArr["bgm"];
		this.soundBgm = null;
		this.isBgmSound = 0;
		PreferenceData.setMusicSwitch(0);
		this.setCtrDisplay();
	}




	public openGameSound() {
		this.isGameSound = 1;
		PreferenceData.setSoundSwitch(1);
		this.setCtrDisplay();
	}

	public closeGameSound() {
		this.isGameSound = 0;
		PreferenceData.setSoundSwitch(0);
		this.setCtrDisplay();
	}


	/**
	 * 修改主界面上声音的图标
	 */
	public ishasSound() {
		if (this.soundBtn.source == "m_sound_png") {
			this.soundBtn.source = "m_sound_close_png";
			this.closeSound();
		} else {
			this.soundBtn.source = "m_sound_png";
			this.openSound();
		}
	}

	/**
	 * 播放声音
	 */
	public playSound(str: string): void {
		if (this.isGameSound == 0) return;
		if (this.isGameSound == 1) {
			new SoundOne(str, this.volume, this).play();
		}
		if (this.soundArr["bgm"] && this.isBgmSound == 1) {
			this.soundArr["bgm"].volumeCtr(this.soundArr["bgm"].volume / 2);
		}
	}
	/**
	 * 打开声音
	 */
	public openSound() {
		this.openSoundCtr();
		this.playBgm();
	}

	/**
	 * 关闭声音 
	 */
	public closeSound() {
		this.closeSoundCtr();
		this.closeBgm();
		for (var i in this.soundArr) {
			if (this.soundArr[i])
				this.soundArr[i].stop();
		}
	}

	/**
	 * 打开滚动面板的声音
	 */
	public openGunSound() {

		if (this.isGameSound == 1) {
			this.soundArr["gunPanle"] = new BGM("gunPanle_mp3", this.volume);
		}
		if (!this.soundArr["bgm"]) return;
		if (this.soundArr["bgm"].volume && this.isBgmSound == 1) {
			this.soundArr["bgm"].volumeCtr(this.soundArr["bgm"].volume / 2);
		}

	}
	/**
	 * 关闭滚动面板的声音
	 */
	public closeGunSound() {
		if (this.isGameSound == 1 && this.soundArr["gunPanle"]) {
			this.soundArr["gunPanle"].stop();
			delete this.soundArr["gunPanle"];
		}
		if (!this.soundArr["bgm"]) return;
		if (this.soundArr["bgm"].volume && this.isBgmSound == 1) {
			this.soundArr["bgm"].volumeCtr(this.volume);
		}
	}

	/**
	 * 音量控制的接口
	 */
	public volumeCtr(num: any) {
		this.volume = num;
		PreferenceData.setSound(num * 100);
		for (var i in this.soundArr) {
			this.soundArr[i].volumeCtr(num);
		}
	}

	/**
	  *  重新调整到原先的音量 
	  */
	public reloadVolume() {
		if (!this.soundArr["bgm"]) return;
		if (this.soundArr["bgm"] && this.isBgmSound == 1) {
			this.soundArr["bgm"].volumeCtr(this.volume);
		}
	}
}